package cz.proks.schoolpupilbook.gae.converter;

import com.google.appengine.api.datastore.KeyFactory;

import cz.proks.schoolpupilbook.gae.dbo.UserDBO;
import cz.proks.schoolpupilbook.shared.domain.User;
import cz.proks.schoolpupilbook.shared.util.Tester;

public class UserConverter implements Converter<UserDBO, User>{
	@Override
	public UserDBO getDBO(User domain) {
		if (domain == null)
			return null;
		
		UserDBO dbo = new UserDBO();
		
		if (!Tester.isEmpty(domain.getId()))
			dbo.setId(KeyFactory.stringToKey(domain.getId()));
		
		dbo.setName(domain.getName());
		dbo.setSurname(domain.getSurname());
		dbo.setUserRole(domain.getRole());
		dbo.setUsername(domain.getUsername());
		dbo.setPassword(domain.getPassword());
		
		return dbo;
	}

	@Override
	public User getDomain(UserDBO dbo) {
		if (dbo == null)
			return null;
		
		User user = new User(KeyFactory.keyToString(dbo.getId()), dbo.getName(), dbo.getSurname(), 
							 dbo.getUserRole(), dbo.getUsername(), dbo.getPassword());
		return user;
	}
}
